package finalproject.mobapp.Main;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import finalproject.mobapp.R;
import finalproject.mobapp.HttpRequestController.CustomHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ARConfigActivity extends Activity {
	
	JSONArray jArray;
	String result = null;
	InputStream is = null;
	StringBuilder sb = null;
	SeekBar distance_seekbar;
	TextView distance_value;
	
	String categorySelected = null;
	int radius = 0;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arconfig);
        
        distance_seekbar = (SeekBar) findViewById(R.id.distance_seekbar);
        distance_value = (TextView) findViewById(R.id.distance_value);
        distance_value.setText("Set Max Distance : " + radius + " meters");
        
        distance_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				radius = progress;
				distance_value.setText("Set Max Distance : " + radius + " meters");
			}
		});
        
        List<String> category_list = new ArrayList<String>();
        category_list.add("all");
        String response = null;
        try {
			response = CustomHttpClient.executeHttpGet(CustomHttpClient.BASE_URL + "getAllCategories");
			
			JSONArray jArray = new JSONArray(response);
			JSONObject json_data = null;
			for (int i=0; i<jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				String cat_name = json_data.getString("name");
				category_list.add(cat_name);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Spinner category_spinner = (Spinner) findViewById(R.id.category_spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category_list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        category_spinner.setAdapter(adapter);
        
        category_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }
    
    public void startARHandle(View v) {
    	Intent intent = new Intent();
    	intent.setClass(this, ARViewActivity.class);
    	intent.putExtra("category", categorySelected);
    	intent.putExtra("radius", radius);
    	startActivity(intent);
    }
    
    public void backHandle(View v) {
    	//Intent intent = new Intent();
    	//intent.setClass(this, MenuActivity.class);
    	
    	ARConfigActivity.this.finish();
    	//startActivity(intent);
    }
    
    public class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
			// TODO Auto-generated method stub
			categorySelected = parent.getItemAtPosition(pos).toString();
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub
			
		}
    	
    }
    
}
